package com.demo.widget;

import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;

public class NAbsoluteLayout extends ViewGroup {

	// params
	private int mPaddingLeft;
	private int mPaddingRight;
	private int mPaddingTop;
	private int mPaddingBottom;

	public NAbsoluteLayout(Context context) {
		super(context);
	}

	public NAbsoluteLayout(Context context, AttributeSet attrs) {
		super(context, attrs);
		mPaddingLeft = attrs.getAttributeIntValue(android.R.attr.paddingLeft, 0);
		mPaddingTop = attrs.getAttributeIntValue(android.R.attr.paddingTop, 0);
		mPaddingRight = attrs.getAttributeIntValue(android.R.attr.paddingRight, 0);
		mPaddingBottom = attrs.getAttributeIntValue(android.R.attr.paddingBottom, 0);
	}

	public NAbsoluteLayout(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
	}

	@Override
	protected void onLayout(boolean changed, int l, int t, int r, int b) {
		int count = getChildCount();
		for (int i = 0; i < count; i++) {
			View child = getChildAt(i);
			if (child.getVisibility() != View.GONE) {
				NAbsoluteLayout.LayoutParams lp = (NAbsoluteLayout.LayoutParams) child.getLayoutParams();
				int childLeft = mPaddingLeft + lp.x;
				int childTop = mPaddingTop + lp.y;
				child.layout(childLeft, childTop, childLeft + child.getMeasuredWidth(), childTop + child.getMeasuredHeight());
			}
		}
	}

	@Override
	protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
		int count = getChildCount();
		int maxHeight = 0;
		int maxWidth = 0;
		measureChildren(widthMeasureSpec, heightMeasureSpec);
		for (int i = 0; i < count; i++) {
			View child = getChildAt(i);
			if (child.getVisibility() != GONE) {
				int childRight;
				int childBottom;

				NAbsoluteLayout.LayoutParams lp = (NAbsoluteLayout.LayoutParams) child.getLayoutParams();
				childRight = lp.x + child.getMeasuredWidth();
				childBottom = lp.y + child.getMeasuredHeight();

				maxWidth = Math.max(maxWidth, childRight);
				maxHeight = Math.max(maxHeight, childBottom);
			}
		}

		maxWidth += mPaddingLeft + mPaddingRight;
		maxHeight += mPaddingTop + mPaddingBottom;

		maxHeight = Math.max(maxHeight, getSuggestedMinimumHeight());
		maxWidth = Math.max(maxWidth, getSuggestedMinimumWidth());

		setMeasuredDimension(resolveSize(maxWidth, widthMeasureSpec), resolveSize(maxHeight, heightMeasureSpec));
	}

	@Override
	protected android.view.ViewGroup.LayoutParams generateDefaultLayoutParams() {
		return new NAbsoluteLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT, 0, 0);
	}

	@Override
	protected ViewGroup.LayoutParams generateLayoutParams(ViewGroup.LayoutParams p) {
		return new NAbsoluteLayout.LayoutParams(p);
	}

	public static class LayoutParams extends ViewGroup.LayoutParams {

		public int x;
		public int y;
		public int width;
		public int height;

		public LayoutParams(Context context, AttributeSet attrs) {
			super(context, attrs);
		}

		public LayoutParams(int width, int height, int x, int y) {
			super(width, height);
			this.x = x;
			this.y = y;
			this.width = width;
			this.height = height;
		}

		public LayoutParams(int width, int height) {
			super(width, height);
		}

		public LayoutParams(ViewGroup.LayoutParams params) {
			super(params);
		}
	}
}
